/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.mallow.impl.model.jpa;

import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Version;
import javax.persistence.Transient;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.MappedSuperclass;

import javax.persistence.PreUpdate;
import javax.persistence.PrePersist;

import javax.persistence.OneToMany;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//import org.springframework.beans.factory.annotation.Autowire;
//import org.springframework.beans.factory.annotation.Required;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Configurable;

//import org.springframework.context.annotation.Scope;
//import org.springframework.context.annotation.ScopedProxyMode;

//
// Hibernate specific annotations.
// http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-hibspec
import org.hibernate.annotations.GenericGenerator;

//import org.jiscinvolve.astrodabis.mallow.model.api.Account;
import org.jiscinvolve.astrodabis.mallow.model.jpa.PersistentObject;
import org.jiscinvolve.astrodabis.mallow.model.jpa.PersistentManager;

/**
 * Base class implementation for PersistentObjects.
 *
 */
@MappedSuperclass
abstract public class PersistentObjectImpl
implements PersistentObject
    {
    /**
     * Our debug logger.
     * 
     */
    private static Log log = LogFactory.getLog(PersistentObjectImpl.class);

    /**
     * Access to our PersistentManager.
     * 
     */
    protected PersistentManagerExtra manager()
        {
        return PersistentManagerImpl.instance();
        }

    /**
     * Protected constructor.
     *
     */
    protected PersistentObjectImpl()
        {
        }

    /**
     * The object identifier.
     *
     */
    @Id
    @Column(name="ident")
    @GeneratedValue(strategy=GenerationType.IDENTITY, generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy="uuid")
    private String ident ;

    @Override
    public String ident()
        {
        return this.ident ;
        }

    @Override
    public void save()
        {
        this.manager().save(
            this
            );
        }

    @Override
    public void delete()
        {
        this.manager().delete(
            this
            );
        }

    @Override
    public boolean equals(Object that)
        {
        if (that != null)
            {
            if (that instanceof PersistentObject)
                {
                return equals(
                    (PersistentObject) that
                    );
                }
            }
        return false ;
        }

    /**
     * Compare objects based on identifiers.
     *
     */
    public boolean equals(PersistentObject that)
        {
        if (that != null)
            {
            return this.ident().equals(
                that.ident()
                );
            }
        return false ;
        }

    }

